

package com.hazelcast.jet.accumulator;

import java.util.Objects;

/**
 * Mutable container of an object reference.
 *
 * @param <T> referenced object type
 * @since Jet 3.0
 */
public class MutableReference<T> {

    private T value;

    /**
     * Creates a new instance with a {@code null} value.
     */
    public MutableReference() {
    }

    /**
     * Creates a new instance with the specified value.
     */
    public MutableReference(T value) {
        this.value = value;
    }

    /**
     * Returns the current value.
     */
    public T get() {
        return value;
    }

    /**
     * Tells whether the current value is {@code null}.
     */
    public boolean isNull() {
        return value == null;
    }

    /**
     * Sets the value as given.
     */
    public MutableReference<T> set(T value) {
        this.value = value;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        return this == o || o != null && this.getClass() == o.getClass() && Objects.equals(this.value, ((MutableReference) o).value);
    }

    @Override
    public int hashCode() {
        return value != null ? value.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "MutableReference(" + value + ')';
    }
}
